import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/**
 * 导入组件，注册路由
 */
import login from '@/page/Login.vue'
import home from '@/page/Home.vue'
import customerManage from '@/page/CustomerManage.vue'
import productList from '@/page/ProductList.vue'
import productClassList from '@/page/ProductClassList.vue'
import addProduct from '@/page/AddProduct.vue'
import dataAnalyze from '@/page/DataAnalyze.vue'
import couponList from '@/page/CouponList.vue'
import promotionList from '@/page/PromotionList.vue'
import receiveCouponList from '@/page/ReceiveCouponList.vue'
import orderList from '@/page/OrderList.vue'

const router = new Router({ 
  routes: [
    {
      path: '/',
      name: 'login',
      component: login
    },
    {
      path: '/home',
      name: 'home',
      component: home,
      redirect:'/dataAnalyze',
      children:[
        {
          path: '/customerManage',
          name: 'customerManage',
          component: customerManage,
          meta: ['客户管理','客户列表']
        },
        {
          path: '/productList',
          name: 'ProductList',
          component: productList,
          meta: ['产品管理','产品列表']
        },{
          path: "/productClassList",
          name:"ProductClassList",
          component:productClassList,
          meta:['产品管理','产品类目']
        },{
          path: "/addProduct",
          name:"AddProduct",
          component:addProduct,
          meta:['产品管理','添加产品']
        },{
          path: "/dataAnalyze",
          name:"DataAnalyze",
          component:dataAnalyze,
          meta:['营销策略','数据分析']
        },{
          path: "/couponList",
          name:"CouponList",
          component:couponList,
          meta:['营销策略','优惠卷']
        },{
          path: "/promotionList",
          name:"PromotionList",
          component:promotionList,
          meta:['营销策略','促销活动']
        },{
          path: "/receiveCouponList",
          name:"ReceiveCouponList",
          component:receiveCouponList,
          meta:['营销策略','优惠卷领取']
        },{
          path: "/orderList",
          name:"OrderList",
          component:orderList,
          meta:['订单管理','订单列表']
        }
      ]
    }
  ]
})

/* 路由拦截器 */
import { getLogin } from '@/utils/localStoreUtils.js'
router.beforeEach((to, from, next) => {//to到哪里去，from从哪里来
  /**
   * 验证是否存在token
   */
  if(to.path == "/"){
    next();
  }else{
    let token = getLogin();
    if(token){
      next();
    }else{
      next({ path: '/' })
    }
  }
})


export default router;